import React, { Component } from 'react'
import Cmp1 from './components/Cmp1'
import Cmp2 from './components/Cmp2'

import MyContext from './context/store'

// 只有在类组件中才有生命周期
export default class App extends Component {
  constructor(props) {
    super(props);
    this.state = {
      // title: '你好世界'
      // 把App中的数据源变成全局
      count: 100
    }
  }


  render() {
    let { count } = this.state
    let setCount = this.setCount.bind(this)
    // App组件是所有组件的根组件，它下面的所有的小弟都可以消费
    return (
      <MyContext.Provider value={{ count, setCount }}>
        <Cmp1 />
        <Cmp2 />
      </MyContext.Provider>
    )
  }

  setCount(n) {
    this.setState(state => {
      return {
        count: state.count+n
      }
    })
  }

}
